package com.ruoyi.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.domain.Coach;
import com.ruoyi.service.ICoachService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 教练Controller
 * 
 * @author ruoyi
 * @date 2025-03-14
 */
@RestController
@RequestMapping("/coach")
public class CoachController extends BaseController
{
    @Autowired
    private ICoachService coachService;

    /**
     * 查询教练列表
     */
    @RequiresPermissions("user:coach:list")
    @GetMapping("/list")
    public TableDataInfo list(Coach coach)
    {
        startPage();
        List<Coach> list = coachService.selectCoachList(coach);
        return getDataTable(list);
    }

    /**
     * 导出教练列表
     */
    @RequiresPermissions("user:coach:export")
    @Log(title = "教练", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coach coach)
    {
        List<Coach> list = coachService.selectCoachList(coach);
        ExcelUtil<Coach> util = new ExcelUtil<Coach>(Coach.class);
        util.exportExcel(response, list, "教练数据");
    }

    @PostMapping("/findAll")
    public R findAll()
    {
        List<Coach> list = coachService.selectCoachList(null);
        return R.ok(list);
    }

    /**
     * 获取教练详细信息
     */
    @RequiresPermissions("user:coach:query")
    @GetMapping(value = "/{coachId}")
    public AjaxResult getInfo(@PathVariable("coachId") Long coachId)
    {
        return success(coachService.selectCoachByCoachId(coachId));
    }

    @PostMapping(value = "/findOne")
    public AjaxResult findOne(@RequestBody Long coachId)
    {
        return success(coachService.selectCoachByCoachId(coachId));
    }



    /**
     * 新增教练
     */
    @RequiresPermissions("user:coach:add")
    @Log(title = "教练", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coach coach)
    {
        return toAjax(coachService.insertCoach(coach));
    }

    /**
     * 修改教练
     */
    @RequiresPermissions("user:coach:edit")
    @Log(title = "教练", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coach coach)
    {
        return toAjax(coachService.updateCoach(coach));
    }

    /**
     * 删除教练
     */
    @RequiresPermissions("user:coach:remove")
    @Log(title = "教练", businessType = BusinessType.DELETE)
	@DeleteMapping("/{coachIds}")
    public AjaxResult remove(@PathVariable Long[] coachIds)
    {
        return toAjax(coachService.deleteCoachByCoachIds(coachIds));
    }

    @PostMapping("/selAll")
    public R<?> selAll() {
        List<Coach> coaches = coachService.selectCoachList(null);
        return R.ok(coaches);
    }

    @PostMapping ("/findCoachforCourseid")
    public R findCoachforCourseid(@RequestBody Long courseId)
    {
        startPage();
        System.out.println(courseId);
        List<Coach> list = coachService.findCoachforCid(courseId);
        return R.ok(list);
    }
}
